<template>
  <el-card class="page-teamList" shadow="always">
    <template #header>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ name: 'teamList' }">新进班组管控</el-breadcrumb-item>
        <el-breadcrumb-item>周报</el-breadcrumb-item>
      </el-breadcrumb>
    </template>
    <el-date-picker
      v-model="datePicker"
      type="daterange"
      range-separator="到"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      @change="change"
    />
    <el-button type="primary" size="default" class="float-right" @click="change">更新</el-button>
    <el-input v-model="text" class="txt" type="textarea" :rows="21" />
  </el-card>
</template>

<script lang="ts" setup>
import useProj from '@renderer/composables/useProj'
import useRecord from '@renderer/composables/useRecord'
import useTeam from '@renderer/composables/useTeam'
import { getDatesInRange } from '@renderer/utils/myDate'
import { ref } from 'vue'
const datePicker = ref('')
const text = ref('')
const { all: allRecord, recordDbs } = useRecord()
const { find: findProj, proj } = useProj()
const { find: findTeam, team } = useTeam()

const change = async () => {
  await allRecord()
  const [date1, date2] = datePicker.value

  const dateArray = getDatesInRange(new Date(date1), new Date(date2))

  let lst = [] as RecordDbModel[]
  recordDbs.value.forEach((re) => {
    const day = new Date(re.date)
    const isEqual = dateArray.some((d) => d.getTime() === day.getTime())
    if (isEqual) {
      lst.push(re)
    }
  })

  // 去重获取team列表
  let set_team = new Set<number>()
  for await (const re of lst) {
    set_team.add(re.teamId)
  }

  //   获取评价合格的team
  let lst_team = [] as TeamModel[]
  for await (const t of [...set_team]) {
    await findTeam(t)
    lst_team.push(team.value!)
  }
  const numTotal = lst_team.length

  let txt = `本周期内${numTotal}个组塔班组进行提级管控，其中`
  let text_hege = '（'

  // 获取工程名称
  let set_proj = new Set<number>()
  for await (const re of lst) {
    set_proj.add(re.projId)
  }
  for await (const pr of [...set_proj]) {
    await findProj(pr)
    const set = new Set(lst.filter((re) => re.projId === pr).map((re) => re.teamId))
    txt += `${proj.value?.shortName}${[...set].length}个，`
    let n = 0
    for await (const t of [...set]) {
      await findTeam(t)
      n = team.value?.evaluate === 1 ? n + 1 : n
    }
    if (n) text_hege += `${proj.value?.shortName}${n}个、`
  }

  txt = txt.replace(/，$/g, '。')
  text_hege = text_hege.replace(/、$/g, '）')

  const numHege = lst_team.filter((t) => t.evaluate === 1).length
  txt += `${numHege}个班组已完成提级管控且评价合格${text_hege}，其余${numTotal - numHege}个班组正在进行提级管控。`
  text.value = txt
}
</script>

<style lang="scss" scoped>
.page-teamList {
  @apply w-full h-full px-5 py-2 bg-slate-100;
  .txt {
    @apply mt-2;
  }
}
</style>
